#version 430
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable

layout (early_fragment_tests) in;

layout (std430,set = 0, binding = 0) buffer bufferVals {
    float brightFactor;
    uint uindex;
} myBufferVals;
layout (set = 1, binding = 0, rgba32ui) uniform writeonly uimageBuffer list_buffer;
layout (set = 1, binding = 1, r32ui) uniform uimage2D head_pointer_image;

layout (set = 2, binding = 0) uniform sampler2D tex;

layout (location = 0) in vec3 inTexCoor;
layout (location = 1) in vec3 frag_position;
layout (location = 0) out vec4 outColor;


void main() {
    uint index;
    uint old_head;
    uvec4 item;
    vec4 color;
    
    color = myBufferVals.brightFactor*textureLod(tex, inTexCoor.xy, 0.0);

    index = atomicAdd(myBufferVals.uindex, 1u);
    old_head = imageAtomicExchange(head_pointer_image, ivec2(gl_FragCoord.xy), uint(index));
    item.x = old_head;
    item.y = packUnorm4x8(color);
    item.z = floatBitsToUint(gl_FragCoord.z);
    item.w = 0;

    imageStore(list_buffer, int(index), item);
    
    outColor = color;
}